const { ccclass, property } = cc._decorator;
import WorldManager from './WorldNodeControl';
import WayBaseControl from './WayBaseControl';
import MyEnums from '../MyEnums'

@ccclass
export default class wallWayControl extends WayBaseControl {
    @property
    length: number = 1;
    @property(cc.SpriteFrame)
    sprite: cc.SpriteFrame = null;
    @property
    maxY: number = 500;

    worldManager: WorldManager = null;
    cameraX_nodeX = 0;

    onLoad() {
        this.setLongth();
        this.setCollider(true, TagWayBody1);
        if (this.node.y > this.maxY) {
            this.node.y = this.maxY
        }
        else if (this.node.y < 0) {
            this.node.y = 0;
        }
        this.cameraX_nodeX = (cc.winSize.width + this.node.width) / 2;
    }
    setLongth() {
        let nodes: Array<cc.Node> = new Array<cc.Node>();
        nodes = this.getNodes();
        if (nodes.length > 0) {
            this.addNodes(nodes);
            this.nodeLength = nodes.length;
            this.indexWidth = nodes[0].width;
            for (let i = 0; i < nodes.length; i++) {
                this.occupyList.push(false);
            }
        }
    }
    private getNodes(): Array<cc.Node> {
        let nodes: Array<cc.Node> = new Array<cc.Node>();
        for (let i = 0; i < this.length; i++) {
            let node = new cc.Node();
            node.group = this.node.group;
            let sprite2 = node.addComponent(cc.Sprite);
            sprite2.spriteFrame = this.sprite;
            nodes.push(node);
        }
        return nodes;
    }
    private addNodes(nodes: Array<cc.Node>) {
        let side = 17;//去侧边
        let nodeWidth: number = (nodes[0].width - side) * nodes.length;
        let nodex = nodeWidth / 2;
        for (let i = 0; i < nodes.length; i++) {
            let node = nodes[i];
            let width = node.width - side;
            node.x = width / 2 - nodex;
            nodex -= width;
            node.name = "way";
            this.node.addChild(node, i);
        }
        this.node.width = nodeWidth + side;
        this.node.height = nodes[0].height;
    }
    update(dt) {
        let hero = HeroHelper.getInstance();
        let xx = Math.abs(this.node.x - hero.cameraPosition.x);
        if (xx < this.cameraX_nodeX) {
            this.worldManager.SetWayActive(true, this.node.uuid, 'wall');
        }
        else {
            this.worldManager.SetWayActive(false, this.node.uuid, 'wall');
        }
    }
}
